const router = require('koa-router')();
const menu = require('../app/controller/menu');
const serverUtil = require('../utils/serverUtil');

/**
 * load menu by user
 */
router.get('/', async function (ctx, next) {

    try {

        let user_id = ctx.request.query.user_id || serverUtil.getLoginUid(ctx);

        if (!user_id) {
            throw new serverUtil.ServerError(serverUtil.code.ILLEGAL_PARAMS);
        }

        let result = await menu.loadMenuByUser(user_id);

        ctx.body = {
            code: serverUtil.code.SUCCESS,
            data: result
        }
    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "query menu list by user failed");
    }
});

/**
 * all menu list
 */
router.get('/list', async function (ctx, next) {

    try {
        let index = ctx.request.query.index || 0;
        let limit = ctx.request.query.limit || 10;
        let result = await menu.loadMenuList(~~index, ~~limit);
        ctx.body = {
            code: serverUtil.code.SUCCESS,
            data: result
        }
    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "query menu list failed");
    }
});

router.get('/parent', async function (ctx, next) {

    try {
        let result = await menu.loadParentMenusList();
        ctx.body = {
            code: serverUtil.code.SUCCESS,
            data: result
        }
    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "query parent menu list failed");
    }
});

/**
 * add menu record
 */
router.post('/add', async function (ctx, next) {

    try {

        let params = ctx.request.body;

        params.parent = params.parent || 0;
        params.type = params.type || 0;
        params.privilege = params.privilege || 0;
        params.order = params.order || 100;

        if (!params.name || (params.type == menu.type.TYPE_LEAF && !params.link)) {
            throw new serverUtil.ServerError(serverUtil.code.ILLEGAL_PARAMS);
        }

        let menu_id = await  menu.addMenu(params);

        if (menu_id > 0) {
            params.id = menu_id;
            ctx.body = {
                code: serverUtil.code.SUCCESS,
                data: params
            }
        } else {
            throw new serverUtil.ServerError(serverUtil.code.FAILED);
        }

    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "add menu record failed");
    }
});

/**
 * delete menu
 */
router.post('/del', async function (ctx, next) {

    try {

        let menu_id = ctx.request.body.menu_id;

        if (!menu_id) {
            throw new serverUtil.ServerError(serverUtil.code.ILLEGAL_PARAMS);
        }

        let in_use = await menu.queryMenuInUse(menu_id);

        if (in_use) {
            throw new serverUtil.ServerError(serverUtil.code.VERIFY_FAILED);
        }

        let del_count = await menu.deleteMenu(menu_id);

        if (del_count > 0) {
            ctx.body = {
                code: serverUtil.code.SUCCESS,
                data: menu_id
            }
        } else {
            throw new serverUtil.ServerError(serverUtil.code.FAILED);
        }

    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "delete menu record failed");
    }
});

/**
 * update menu
 */
router.post('/update', async function (ctx, next) {

    try {

        let params = ctx.request.body;

        if (!params.id) {
            throw new serverUtil.ServerError(serverUtil.code.ILLEGAL_PARAMS);
        }


        params.parent = params.parent || 0;
        params.type = params.type || 0;
        params.privilege = params.privilege || 0;
        params.order = params.order || 100;

        let db_menu = await menu.loadMenuById(params.id);

        if(!db_menu || db_menu.length == 0 || db_menu[0].disabled){
            throw new serverUtil.ServerError(serverUtil.code.VERIFY_FAILED);
        }

        if(db_menu[0].type == menu.type.TYPE_PARENT && params.type  == menu.type.TYPE_LEAF ){
            let hasChild = await menu.queryChildCount(params.id);
            if(hasChild){
                throw new serverUtil.ServerError(serverUtil.code.VERIFY_FAILED);
            }
        }


        let update_count = await menu.updateMenu(params);


        if (update_count > 0) {
            ctx.body = {
                code: serverUtil.code.SUCCESS,
                data: params
            }
        } else {
            throw new serverUtil.ServerError(serverUtil.code.FAILED);
        }

    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "update menu record failed");
    }
});

/**
 * auth menu for user
 */
router.post('/auth', async function (ctx, next) {

    try {

        let admin_id = ctx.request.body.admin_id;
        let menu_ids = ctx.request.body.menu_ids || [];

        if (!admin_id) {
            throw new serverUtil.ServerError(serverUtil.code.ILLEGAL_PARAMS);
        }


        let ids = await  menu.authMenu(admin_id, menu_ids);

        if (ids.length == menu_ids.length) {
            ctx.body = {
                code: serverUtil.code.SUCCESS,
                data: ids
            }
        } else {
            throw new serverUtil.ServerError(serverUtil.code.FAILED);
        }

    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "auth menu for admin failed");
    }
});

module.exports = router;